package com.tianqu.tms.core.util.car;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.exception.TmsDaoException;
import com.tianqu.tms.core.util.HttpUtil;
import com.tianqu.tms.core.util.MD5Util;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.dto.MarketCarInfoChe300;
import com.tianqu.tms.entity.Car;
import com.tianqu.tms.entity.Codes;
import com.tianqu.tms.www.api.network.PSHTTPUtil;

public class Che300ApiUtils {
	private Logger LOGGER = Logger.getLogger(Che300ApiUtils.class);
	
	private static final String USER_NAME = "dalianusedcar";
	private static final String TOKEN = "083d9c7acc165e7536073918b1a59406";
	private static final String URL_LOGIN = "http://testapi.che300.com/service/car";
	private static HttpUtil HTTP = new HttpUtil();
	private static Che300ApiUtils instance = null;

	public static Che300ApiUtils getInstance() {
		if (null == instance)
			instance = new Che300ApiUtils();
		return instance;
	}
	
	public Che300Response pushCar(String serial) throws TmsDaoException {
		MarketCarInfoChe300 carInfo = Car.getInstance().getCarInfobySerial(serial);
		if(carInfo == null) LOGGER.error("not found car info,serial:"+serial);
		return pushCar(USER_NAME,TOKEN,convert(carInfo));
	}

	public Che300CarInfoJsonBean convert(MarketCarInfoChe300 bean) throws TmsDaoException{
		Che300CarInfoJsonBean info = null;
		if(bean!=null){
			Codes code = Codes.getInstance();
			
			String color = code.getItemValueByItemIdAndItemCode(Codes.VEHICLE_COLOR,bean.getColor());
			
			String carLevel = code.getItemValueByItemIdAndItemCode(code.VEHICLE_TYPE,bean.getCarType());
			
			String market = "";//10：大连，20：长春，30：哈尔滨，40：乌兰浩特，21：厦门
			if("10".equals(TmsConstants.MARKET_NO)) market="大连";
			else if("20".equals(TmsConstants.MARKET_NO)) market="长春";
			else if("30".equals(TmsConstants.MARKET_NO)) market="哈尔滨";
			else if("40".equals(TmsConstants.MARKET_NO)) market="乌兰浩特";
			else if("21".equals(TmsConstants.MARKET_NO)) market="厦门";
			else if("17".equals(TmsConstants.MARKET_NO)) market="市场一";
			else if("18".equals(TmsConstants.MARKET_NO)) market="市场二";
			else if("19".equals(TmsConstants.MARKET_NO)) market="市场三";
			else if("50".equals(TmsConstants.MARKET_NO)) market="乌兰浩特嘉尧二手车市场";
			else  market="未知";
			
			info = new Che300CarInfoJsonBean();
			info.setCar_id(bean.getCarId());
			info.setOperation("add");
			info.setCar_no(bean.getCarNo());
			info.setSeller_type(bean.getSellerType());
			info.setVin(bean.getVin());
			info.setBrand(bean.getBrand());
			info.setSeries(bean.getSeries());
			info.setModel(bean.getModel());
			info.setModel_year(bean.getModelYear());
			info.setCar_level(carLevel);
			info.setProvince(bean.getProvince());
			info.setCity(market);
			info.setLiter(bean.getLiter());
			info.setEmission_standard(bean.getEmissionStandard());
			info.setGear_type(bean.getGearType());
			info.setPrice(bean.getPrice());
			info.setMileage(bean.getMileage());
			info.setRegister_date(bean.getRegisterDate());
			info.setInspect_date(bean.getInspectDate());
			info.setAudit_date(bean.getAuditDate());
			info.setPost_time(bean.getPostTime());
			info.setStatus(bean.getStatus());
			info.setColor(color);
			info.setTransfer_times(bean.getTransferTimes());
			info.setContactor(bean.getContactor());
			info.setPhone(bean.getPhone());
			info.setCar_url(bean.getCarUrl());
			StringBuffer pic = new StringBuffer("");
			if(StringUtil.isNotBlank(bean.getPictures1())) pic.append(bean.getPictures1());
			if(StringUtil.isNotBlank(bean.getPictures2())) pic.append(",").append(bean.getPictures2());
			if(StringUtil.isNotBlank(bean.getPictures3())) pic.append(",").append(bean.getPictures3());
			if(StringUtil.isNotBlank(bean.getPictures4())) pic.append(",").append(bean.getPictures4());
			if(StringUtil.isNotBlank(bean.getPictures5())) pic.append(",").append(bean.getPictures5());
			if(StringUtil.isNotBlank(bean.getPictures6())) pic.append(",").append(bean.getPictures6());
			if(StringUtil.isNotBlank(bean.getPictures7())) pic.append(",").append(bean.getPictures7());
			if(StringUtil.isNotBlank(bean.getPictures8())) pic.append(",").append(bean.getPictures8());
			info.setPictures(pic.toString());
			info.setInspect_status(true);
			info.setInsurance_date(bean.getInsuranceDate());
			info.setInspect_date(bean.getInspectDate());
			info.setInspection_report_url(bean.getInspectionReportUrl());
			info.setInspector_description(bean.getInspectorDescription());
			info.setCar_description(bean.getCarDescription());
			info.setCar_no(bean.getCarNo());
	    }
		return info;
	}
	
	public Che300Response pushCar(String user, String token, Che300CarInfoJsonBean info) {
		if(info == null)  return null;
		try {
			String car = PSHTTPUtil.convertJavaBeanToJson(info);
			String timestamp = String.valueOf(new Date().getTime() / 1000);
			Map<String, Object> params = new HashMap<String, Object>();
			params.put("user", user);
			params.put("time", timestamp);
			params.put("code", MD5Util.getMD5String(token + timestamp));
			params.put("oper", "pushCar");
			params.put("car",car );
			String r = HTTP.doPost(URL_LOGIN, params);
			Che300Response res = PSHTTPUtil.convertJsonToJavaBean(r, Che300Response.class);
			return res;
		} catch (Exception e) {
			LOGGER.error(e);
		}
		return null;
	}

//	public static void main(String[] args) throws Exception {
//		Che300CarInfoJsonBean info = new Che300CarInfoJsonBean();
//		info.setCar_id("CAR123233");
//		info.setOperation("add");
//		info.setCar_no("BJ-1198");
//		info.setSeller_type(2);
//		info.setVin("LFBRF778757A02006");
//		info.setBrand("丰田");
//		info.setSeries("兰德酷路泽");
//		info.setModel("2005款 一汽丰田兰德酷路泽 4.7L 自动VX-R");
//		info.setModel_year(2005);
//		info.setCar_level("大型车");
//		info.setProvince("上海");
//		info.setCity("上海");
//		info.setLiter("4.7L");
//		info.setEmission_standard("国5");
//		info.setGear_type("手自一体");
//		info.setPrice(32.80f);
//		info.setMileage(7.0f);
//		info.setRegister_date("2005-12-01");
//		info.setInspect_date("2005-12-01");
//		info.setAudit_date("2005-12-01");
//		info.setPost_time("2016-04-03 14:32:12");
//		info.setStatus("在售");
//		info.setColor("黑色");
//		info.setTransfer_times(1);
//		info.setContactor("张先生");
//		info.setPhone("13825458547");
//		info.setCar_url("http://www.haoche.com/car/CAR123233");
//		info.setPictures(
//				"http://www.haoche.com/car/pic_1,http://www.haoche.com/car/pic_2,http://www.haoche.com/car/pic_3");
//		info.setInspect_status(true);
//		info.setInspect_date("2016-03-27");
//		info.setInspection_report_url("http://www.haoche.com/report/CAR123233");
//		info.setInspector_description("经检测，该车右后车门、后保险杠有可见伤，全车原厂漆，加装导航、倒车影像、皮座椅，变速箱轻微渗油");
//		info.setCar_description("5门8座SUV，四轮驱动，手自一体，多功能方向");
//
//		Che300Response r = Che300ApiUtils.getInstance().pushCar("dalianusedcar", "083d9c7acc165e7536073918b1a59406", info);
//		System.out.println(r.getStatus());
//	}
}
